﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Thor.Domain.Configuration;
using Thor.WebRole.Services;

namespace Thor.Silverlight.Views
{
    public partial class Calendars : Page
    {
        public Calendars()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            var window = new NewCalendar();
            window.Closed += new EventHandler(window_Closed);
            window.Show();
        }

        void window_Closed(object sender, EventArgs e)
        {
            var window = sender as NewCalendar;

            if (window.DialogResult == true)
            {
                var context = domainDataSource1.DomainContext as CalendarManagerDomainContext;
                context.Calendars.Add(window.Calendar);
                this.dataGrid1.ItemsSource = context.Calendars;
            }
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.dataGrid1.SelectedItem != null && this.dataGrid1.SelectedItem is Thor.Domain.Calendar)
            {
                Thor.Domain.Calendar calendar = this.dataGrid1.SelectedItem as Thor.Domain.Calendar;
                var context = domainDataSource1.DomainContext as CalendarManagerDomainContext;
                context.Calendars.Remove(calendar);
            }
        }

        private void okButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.domainDataSource1.HasChanges)
                this.domainDataSource1.SubmitChanges();
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.domainDataSource1.HasChanges && this.domainDataSource1.CanLoad)
                this.domainDataSource1.Load();
        }
    }
}
